package monitor

import (
	"gitee.com/rw555/fdsmastergo/pkg/logger"
	"gitee.com/rw555/fdsmastergo/pkg/state"
	"go.uber.org/zap"
)

// powerAdjust 功率调整策略
func (m *Monitor) powerAdjust(lastTemp, currentTemp int) {
	isRunning, err := m.device.PumpIsRunning()
	if err != nil {
		logger.SystemError("获取泵状态失败", zap.Error(err))
		return
	}

	if !isRunning {
		return
	}

	if lastTemp == currentTemp {
		return
	}

	// 使用读锁保护配置访问
	m.configMutex.RLock()
	low := int(m.systemConfig["monitor.temp_threshold.open_pump"].(float64)) - int(m.systemConfig["monitor.temp_threshold.open_pump_back_value"].(float64))
	high := int(m.systemConfig["monitor.temp_threshold.open_pump"].(float64)) + int(m.systemConfig["monitor.temp_threshold.open_pump_back_value"].(float64))
	m.configMutex.RUnlock()

	if currentTemp > high {
		if currentTemp > lastTemp {
			m.power += int(m.systemConfig["monitor.power_adjust.duty_step"].(float64))
			if m.power > int(m.systemConfig["monitor.power_adjust.duty_max"].(float64)) {
				m.power = int(m.systemConfig["monitor.power_adjust.duty_max"].(float64))
			}
			m.device.SetPumpPower(m.power)
			logger.SystemInfo("泵运行中，温度上升", zap.Int("lastTemp", lastTemp), zap.Int("currentTemp", currentTemp))
		} else {
			logger.SystemInfo("泵运行中，温度下降", zap.Int("lastTemp", lastTemp), zap.Int("currentTemp", currentTemp))
		}
	} else if currentTemp < low {
		if currentTemp > lastTemp {
			m.power -= int(m.systemConfig["monitor.power_adjust.duty_step"].(float64))
			if m.power < int(m.systemConfig["monitor.power_adjust.duty_min"].(float64)) {
				m.power = int(m.systemConfig["monitor.power_adjust.duty_min"].(float64))
			}
			m.device.SetPumpPower(m.power)
			logger.SystemInfo("泵运行中，温度上升", zap.Int("lastTemp", lastTemp), zap.Int("currentTemp", currentTemp))
		} else {
			logger.SystemInfo("泵运行中，温度下降", zap.Int("lastTemp", lastTemp), zap.Int("currentTemp", currentTemp))
		}
	}
}

func (m *Monitor) temperatureControl(currentTemp int) {
	// 使用读锁保护配置访问
	m.configMutex.RLock()
	highFatal := int(m.systemConfig["monitor.temp_threshold.high_fatal"].(float64))
	closeHeater := int(m.systemConfig["monitor.temp_threshold.close_heater"].(float64))
	openPump := int(m.systemConfig["monitor.temp_threshold.open_pump"].(float64))
	closePumpForce := int(m.systemConfig["monitor.temp_threshold.close_pump_force"].(float64))
	openHeater := int(m.systemConfig["monitor.temp_threshold.open_heater"].(float64))
	m.configMutex.RUnlock()

	if currentTemp > highFatal {
		if ok, err := m.device.PumpIsRunning(); ok && err == nil {
			m.device.ClosePump()
			m.power = 0
			m.device.SetPumpPower(m.power)
		}
		if ok, err := m.device.IsHeater1Open(); ok && err == nil {
			m.device.CloseHeater1()
		}
		if ok, err := m.device.IsHeater2Open(); ok && err == nil {
			m.device.CloseHeater2()
		}
		logger.SystemInfo("温度过高，关闭泵和加热器", zap.Int("currentTemp", currentTemp))
	} else if currentTemp > closeHeater {
		if m.mode.HasMode(state.ModeAuto) {
			if ok, err := m.device.IsHeater1Open(); ok && err == nil {
				m.device.CloseHeater1()
			}
			if ok, err := m.device.IsHeater2Open(); ok && err == nil {
				m.device.CloseHeater2()
			}
		}
	} else if currentTemp > openPump {
		if m.mode.HasMode(state.ModeAuto) {
			if ok, err := m.device.PumpIsRunning(); !ok && err == nil {
				m.device.OpenPump()
				m.power = int(m.systemConfig["monitor.power_adjust.duty_default"].(float64))
				m.device.SetPumpPower(m.power)
			}
		}
	} else if currentTemp < closePumpForce {
		if m.mode.HasMode(state.ModeAuto) {
			if ok, err := m.device.PumpIsRunning(); ok && err == nil {
				m.device.ClosePump()
			}
		}
	} else if currentTemp < openHeater {
		if m.mode.HasMode(state.ModeAuto) {
			if ok, err := m.device.IsHeater1Open(); !ok && err == nil {
				m.device.OpenHeater1()
			}
			if ok, err := m.device.IsHeater2Open(); !ok && err == nil {
				m.device.OpenHeater2()
			}
		}
	}
}

func (m *Monitor) TimerProc() {
	lastTemp := m.currentTemp
	m.currentTemp = m.device.GetTemp()
	//logger.SystemInfo("当前温度", zap.Int("last temperature", lastTemp), zap.Int("current temperature", m.currentTemp))

	// 温度控制策略
	m.temperatureControl(m.currentTemp)

	// 如果是自动模式，执行自动控制策略
	if m.mode.HasMode(state.ModeAuto) {
		m.powerAdjust(lastTemp, m.currentTemp)
		m.UpdateAutoModeInfo()
	}
}
